package com.cyy.system.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.cyy.common.domain.Response;
import com.cyy.common.pojo.dto.common.OptionDto;
import com.cyy.common.pojo.dto.RoleFormDto;
import com.cyy.common.service.RoleMenuService;
import com.cyy.common.service.RoleService;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author caoyunyou
 */
@Tag(name = "菜单")
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
@SaCheckLogin
public class RoleController {

    private final RoleService roleService;

    private final RoleMenuService roleMenuService;


    @GetMapping("/page")
    public Response getRolePage(@RequestParam(value = "keywords",required = false) String keywords) {
        return Response.success().data(roleService.getPage(keywords));
    }

    @GetMapping("/options")
    public Response getRoleOptions(@RequestParam(value = "keywords",required = false) String keywords) {
        List<OptionDto<String, Integer>> res = roleService.getRoleOptions(keywords);
        return Response.success().data(res);
    }

    @GetMapping("/{roleId}/menuIds")
    public Response getRoleMenuIds(@PathVariable("roleId") Integer roleId) {
        List<Integer> res = roleMenuService.getRoleMenuIds(roleId);
        return Response.success().data(res);

    }

    @PutMapping("/{roleId}/menus")
    public Response updateRoleMenus(@PathVariable("roleId") Integer roleId,
                                    @RequestBody @NotNull List<Integer> menuIds) {
        roleMenuService.updateRoleMenus(roleId, menuIds);
        return Response.success();
    }

    @GetMapping("/{id}/form")
    public Response getRoleForm(@PathVariable("id")  Integer id) {
        RoleFormDto res = roleService.getRoleForm(id);
        return Response.success().data(res);
    }

    @PostMapping("")
    public Response addRole(@RequestBody RoleFormDto req) {
        roleService.addRole(req);
        return Response.success();
    }


    @PutMapping("")
    public Response updateRole(@RequestBody RoleFormDto req) {
        roleService.updateRole(req);
        return Response.success();
    }

    @DeleteMapping("/{ids}")
    public Response deleteRoles(@PathVariable("ids")  String ids) {
        roleService.deleteRoles(ids);
        return Response.success();
    }
}
